package com.fj.netty.chat.handler;

import com.fj.netty.chat.message.GroupChatRequestMessage;
import com.fj.netty.chat.server.service.ServiceFactory;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.Set;

/**
 * @author fj
 * @date 2024/1/12 13:43
 */
@Slf4j
public class GroupChatRequestMessageHandler extends SimpleChannelInboundHandler<GroupChatRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GroupChatRequestMessage msg) throws Exception {
        Set<Channel> channelSet = ServiceFactory.getGroupSession().memberChannels(msg.getGroupName());
        if (channelSet == null || channelSet.isEmpty()){
            log.debug("群组不存在或成员为空");
            return;
        }
        for (Channel channel:channelSet){
            channel.writeAndFlush(msg);
        }
    }
}
